//
//  ZFBindCardViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/25.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFBindCardViewController.h"
#import "ZFFaceInfoViewController.h"
#import "UIButton+countDown.h"
#import "ValuePickerView.h"
#import "ZFHeader.h"

@interface ZFBindCardViewController ()<UITextFieldDelegate>
{
    NSString *startTime;
    NSArray *bankTypeArray;
}
@property (nonatomic,strong) ValuePickerView *pickerV;
@property (nonatomic,strong) AnimatingView *animation;
@property (nonatomic,strong) AlertBoxView *alertV;

@end

@implementation ZFBindCardViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"bingCardView"];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"bingCardView"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑定银行卡";
    [self initData];
    [_bindCardButton setShadowWidth:2 shadowHeight:2 shadowColor:Main_Color1 radius:10];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"true_name"]) {
        
        _nameTF.enabled = NO;
        _nameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"true_name"];
    }
}

- (void)initData
{
    startTime = [ZFPulicAPI getCurrentTimestamp];
    
    _animation = [[AnimatingView alloc] init];
    
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];

    bankTypeArray = @[@"建设银行",@"中国银行",@"邮储银行",@"重庆农村商业银行",@"工商银行",@"广大银行",@"交通银行",@"农业银行",@"兴业银行",@"招商银行",@"建设银行",@"中信银行"];
    _pickerV = [[ValuePickerView alloc] init];
}
#pragma mark ———— textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;

    if (_bankcardTF == textField) {
        
        if (proposedNewLength > 20) {
            
            return NO;
        }
    }
    if (_phoneTF == textField) {
        
        if (proposedNewLength > 11) {
            
            return NO;
        }
    }
    if (_verrityTF == textField) {
        
        if (proposedNewLength > 6) {
            
            return NO;
        }
    }
    return YES;
}
#pragma mark ———— button的点击事件
- (IBAction)chooseBankType:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    _pickerV.dataSource = bankTypeArray;
    _pickerV.pickerTitle = @"请选择银行";
    _pickerV.valueDidSelect = ^(NSString *value) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        NSArray * stateArr = [value componentsSeparatedByString:@"/"];
        [button setTitle:stateArr[0] forState:UIControlStateNormal];
    };
    [_pickerV show];
}

//获取验证码
- (IBAction)getVerityCode:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    
    if ([ZFPulicAPI replaceBlank:_phoneTF.text].length >= 11 && [ZFLoginConfig isValidatePhone:[ZFPulicAPI replaceBlank:_phoneTF.text]]) {
        
        [button startWithTime:60 title:@"重获验证码" countDownTitle:@"s后重新获取" mainColor:Main_Color1 countColor:[UIColor lightGrayColor]];
        [ZFLoginHttpRequest requestGetSmsCodePhone:[ZFPulicAPI replaceBlank:_phoneTF.text] withCodeType:@"5" sucessBlock:^(NSDictionary *dict) {
            
        } failureBlock:^(NSInteger failSign) {
            [_alertV appearAndDisappearAlertWithString:@"网络连接失败" ];
        }];
    }
    else
    {
        [_alertV appearAndDisappearAlertWithString:@"请输入正确的手机号码" ];
    }
}

//确定绑定
- (IBAction)sureBind:(id)sender {
    
    if ([self judgeSubmitinfo]) {
        
        ZFUserBankCardInfoModal *modal = [ZFUserBankCardInfoModal new];
        modal.owner_name = [ZFPulicAPI replaceBlank:_nameTF.text];
        modal.bank_name = _chooseBankTypeButton.titleLabel.text;
        modal.bank_no = [ZFPulicAPI replaceBlank:_bankcardTF.text];
        modal.bank_phone = [ZFPulicAPI replaceBlank:_phoneTF.text];
        modal.sms_code = [ZFPulicAPI replaceBlank:_verrityTF.text];
        modal.startTime = startTime;
        
        [_animation startTheSmallAnimation];
        [ZFApplyInfoHttpRequest requestSubmitUserBankCardInfo:modal smsCode:[ZFPulicAPI replaceBlank:_verrityTF.text] sucessBlock:^(NSDictionary *dict) {
            
            [_animation stopTheSmallAnimation];
            if ([dict[@"is_alert"] integerValue] == 1) {
                
                [_alertV appearAndDisappearAlertWithString:dict[@"msg"]];
            }
            if ([dict[@"code"] integerValue] == 1) {
                
                ZFFaceInfoViewController *faceVC = [ZFFaceInfoViewController new];
                [self.navigationController pushViewController:faceVC animated:YES];
            }

        } failureBlock:^(NSInteger failSign) {
            
            [_animation stopTheSmallAnimation];
            [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
    }
}
#pragma mark ———— 判断提交信息
- (BOOL)judgeSubmitinfo
{
    if ([ZFPulicAPI replaceBlank:_nameTF.text].length > 0 && [ZFPulicAPI replaceBlank:_bankcardTF.text].length > 0 && [ZFPulicAPI replaceBlank:_phoneTF.text].length >= 11 && [ZFPulicAPI replaceBlank:_verrityTF.text].length >= 6 && ![_chooseBankTypeButton.titleLabel.text isEqualToString:@"请选择银行"]) {
        
        return YES;
    }
    [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
    return NO;
}
#pragma mark ———— 返回
- (BOOL)navigationShouldPopOnBackButton
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self.navigationController popToRootViewControllerAnimated:YES];
    if (delegate.isFirstEnterApp == YES) {
        
        delegate.isFirstEnterApp = NO;
        return NO;
    }
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
